//
//  ProfilePushView.swift
//  MyTest
//
//  Created by guanzhongwang on 2021/5/8.
//

import UIKit

class ProfilePushView: UIView {

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
    private var descLabel = UILabel()
    private let titles:[String] = ["你创作的,就是头条","记录新鲜事","在小的想法,都值得被记录"]
    private var index = 0
    init() {
        super.init(frame: CGRect.zero)
        self.createUI()
    }
    func createUI() {
        for sv in subviews {
            sv.removeFromSuperview()
        }
        if DataManager.login {
            backgroundColor = .white
            let tipLabel = UILabel()
            addSubview(tipLabel)
            tipLabel.text = "创作中心"
            tipLabel.font = getLabelFont(15,blod: true)
            tipLabel.snp.makeConstraints({
                $0.left.equalTo(15)
                $0.top.equalTo(15)
            })
            let arrow = UIImageView()
            let image = UIImage(named: "arrowright")?.withRenderingMode(UIImage.RenderingMode.alwaysTemplate)
            arrow.image = image
            arrow.tintColor = .lightGray
            
            let infobtn = UIButton(type: .custom)
            infobtn.setTitle("进入", for: .normal)
            infobtn.titleLabel?.font = Font_Size_12
            infobtn.setTitleColor(B999_TITLE_COLOR, for: .normal)
            addSubview(infobtn)
            addSubview(arrow)
            arrow.snp.makeConstraints({
                $0.right.equalToSuperview().inset(15)
                $0.centerY.equalTo(tipLabel)
            })
            
            infobtn.snp.makeConstraints({
                $0.right.equalTo(arrow.snp.left).offset(3)
                $0.centerY.equalTo(tipLabel)
            })
            
            infobtn.addTarget(self, action: #selector(ideavc), for: .touchUpInside)
            
            let serviceView =  ServiceView()
            addSubview(serviceView)
            serviceView.delegate = self
            serviceView.setMenus(menus: [ProfileMenuModel.init(title: "创作首页", imageName: "Idea", className: "IdeaViewController"),ProfileMenuModel.init(title: "数据助手", imageName: "shuju"),ProfileMenuModel.init(title: "收益提现", imageName: "tixian"),ProfileMenuModel.init(title: "草稿箱", imageName: "newsbox")])
            serviceView.snp.makeConstraints({
                $0.left.equalTo(0)
                $0.width.equalTo(ScreenWidth-30)
                $0.top.equalTo(tipLabel.snp.bottom).offset(15)
                $0.bottom.equalToSuperview()
            })
        }else{
            backgroundColor = .white
            descLabel = UILabel()
            descLabel.text = titles[index]
            descLabel.font = Font_Size_14
            addSubview(descLabel)
            descLabel.snp.makeConstraints({
                $0.left.top.equalTo(15)
                $0.bottom.equalToSuperview().inset(15)
            })
            let pushBtn = UIButton(type: .custom)
            addSubview(pushBtn)
            pushBtn.setTitle("发布", for: .normal)
            pushBtn.setTitleColor(RED_COLOR, for: .normal)
            pushBtn.titleLabel?.font = Font_Size_14
            var bimage = UIImage(named: "add")
            bimage =  bimage?.withRenderingMode(UIImage.RenderingMode.alwaysTemplate)
            pushBtn.tintColor = RED_COLOR
            pushBtn.setImage(bimage, for: .normal)
            pushBtn.layer.cornerRadius = 10
            pushBtn.layer.borderWidth = 1
            pushBtn.layer.borderColor = RED_COLOR.cgColor
            pushBtn.snp.makeConstraints({
                $0.right.equalTo(15)
                $0.centerY.equalToSuperview()
                $0.width.equalTo(70)
            })
            pushBtn.addTarget(self, action: #selector(pushclick), for: .touchUpInside)
        }
    }
    func changeText() {
        index += 1
        if index == titles.count {
            index = 0
        }
        descLabel.text = titles[index]
    }
    @objc func ideavc(){
        let fvc = fatherViewController() as? CommonViewController
        fvc?.pushViewController(IdeaViewController(), animated: true)
        fvc?.hidesBottomBarWhenPushed = true
    }
    @objc func pushclick(){
        self.changeText()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
extension ProfilePushView:MenuServiceDelegate{
    func itemSelected(className: String, title: String) {
        if className != "" {
            let vc = String.ClassFromString(className)
            vc?.title = title
            let fvc = fatherViewController() as? CommonViewController
            fvc?.pushViewController(vc!, animated: true)
            fvc?.hidesBottomBarWhenPushed = true
        }
    }
}
